package com.demo.third;

import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.demo.wx.cp.aes.HttpUt;
import com.github.binarywang.demo.wx.cp.controller.ThirdBaseController;

/**
 * created by lanxinghua@2dfire.com on 2020/2/15
 * 第三方授权登录
 */
public class ThirdAuthenticationDemo extends ThirdBaseController {
    public static void main(String[] args) throws Exception{
        // http://chenxingxing.51vip.biz/oauth?code=RXgn-95J2DaBZQeKYIrRMV0tZuYFbSwwA4sk-5fzuQA&state=STATE
        JSONObject result = getUserInfo3rd("lPQMhrjqKF5nNxRyGO2Vnsr_tXoyC72xAid7vKMbOLA");
        String user_ticket = result.getString("user_ticket");
        JSONObject userInfo = getUserDetail3rd(user_ticket);
        System.out.println(userInfo.toJSONString());
    }

    /**
     * {
     *     "errcode": 0,
     *     "user_ticket": "wSazTq6CqQXpHN3F6Eh2WKDulcZFj4hS7331xRNNUuY6fq-xN43Bd-wQizuYbSJFr_6tkNfuxKJtng",
     *     "CorpId": "ww4007bc8885",
     *     "UserId": "lanxinghre.com",
     *     "DeviceId": "d93b8209-bf15-9bd2-136138799a03",
     *     "errmsg": "ok",
     *     "expires_in": 1800,
     *     "parents": []
     * }
     *
     * {
     *     "errcode": 0,
     *     "corpid": "ww4007bbede5885",
     *     "gender": "1",
     *     "name": "lanxa@2.com",
     *     "errmsg": "ok",
     *     "avatar": "https://rl.com/node/wwmng/wwmng/style/images/independent/DefaultAvatar$73ba92b5.png",
     *     "department": [110632191, 148554685],
     *     "userid": "lanxire.com"
     * }
     */

    // 网页授权登录

    /**
     * 获取访问用户身份
     * @throws Exception
     */
    public static JSONObject getUserInfo3rd(String code) throws Exception{
        String url = "https://qyapi.weixin.qq.com/cgi-bin/service/getuserinfo3rd?suite_access_token=%s&code=%s";
        JSONObject result = HttpUt.sendGet(String.format(url, getSuiteAccessToken(), code));
        System.out.println(result.toJSONString());
        return result;
    }


    /**
     * 获取访问用户敏感信息
     * @throws Exception
     */
    public static JSONObject getUserDetail3rd(String user_ticket) throws Exception{
        String url = "https://qyapi.weixin.qq.com/cgi-bin/service/getuserdetail3rd?suite_access_token=%s";
        JSONObject object = new JSONObject();
        object.put("user_ticket", user_ticket);
        JSONObject result = HttpUt.sendPost(String.format(url, getSuiteAccessToken()), object.toJSONString());
        System.out.println("用户详细信息："+result.toJSONString());
        return result;
    }

    // 扫码授权登录
}
